package cn.piggy.gateway.core.auth;

import cn.piggy.gateway.common.util.JwtUtil;
import io.jsonwebtoken.Claims;
import java.util.Date;
import java.util.List;


public class AuthService implements Auth {
    @Override
    public boolean validate(String token,String role) {
        try {
            Claims decode = JwtUtil.decode(token);
            Date expiration = decode.getExpiration();
            Date now = new Date(System.currentTimeMillis());
            List<String> roles = (List<String>)decode.get("roles");
            // expiration > now = true
            return expiration.after(now) && roles.contains(role);
        }catch (Exception e){
            return false;
        }
    }

}
